package cn.ocars.playgame.pencilstudy

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import cn.ocars.playgame.pencilstudy.ui.components.PracticeType
import cn.ocars.playgame.pencilstudy.ui.components.WelcomeScreen
import cn.ocars.playgame.pencilstudy.ui.components.WritingCardDemo
import cn.ocars.playgame.pencilstudy.ui.components.LowLatencyWritingCardDemo
import cn.ocars.playgame.pencilstudy.ui.components.SimplifiedWritingDemo
import cn.ocars.playgame.pencilstudy.ui.components.MultimodalGemmaDemo
import cn.ocars.playgame.pencilstudy.ui.theme.一年级书法Theme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            一年级书法Theme {
                var showWritingCardDemo by remember { mutableStateOf(false) }
                var showLowLatencyDemo by remember { mutableStateOf(false) }
                var showMiniCPMVDemo by remember { mutableStateOf(false) }
                var showMultimodalGemmaDemo by remember { mutableStateOf(false) }
                var currentPracticeType by remember { mutableStateOf("words") } // 默认为词语练习
                
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    
                    when {
                        showMultimodalGemmaDemo -> {
                            MultimodalGemmaDemo(
                                onBackClick = { showMultimodalGemmaDemo = false },
                                modifier = Modifier.padding(innerPadding)
                            )
                        }
                        showMiniCPMVDemo -> {
                            SimplifiedWritingDemo(
                                onBackClick = { showMiniCPMVDemo = false },
                                modifier = Modifier.padding(innerPadding)
                            )
                        }
                        showLowLatencyDemo -> {
                            LowLatencyWritingCardDemo(
                                onBackClick = { showLowLatencyDemo = false },
                                practiceType = currentPracticeType,
                                modifier = Modifier.padding(innerPadding)
                            )
                        }
                        showWritingCardDemo -> {
                            WritingCardDemo(
                                onBackClick = { showWritingCardDemo = false },
                                modifier = Modifier.padding(innerPadding)
                            )
                        }
                        else -> {
                            WelcomeScreen(
                                onPracticeTypeSelected = { practiceType ->
                                    // 处理练习类型选择
                                    // 根据不同的练习类型，选择不同的演示界面
                                    when (practiceType.title) {
                                        "数字练习", "字母练习", "汉字练习" -> {
                                            // 使用MiniCPM-V 2.5模型
                                            showMiniCPMVDemo = true
                                        }
                                        "笔画练习" -> {
                                            // 使用低延迟书写卡片
                                            currentPracticeType = "strokes"
                                            showLowLatencyDemo = true
                                        }
                                        "偏旁练习" -> {
                                            // 使用低延迟书写卡片
                                            currentPracticeType = "radicals"
                                            showLowLatencyDemo = true
                                        }
                                        "词语练习" -> {
                                            // 对于复杂书写，使用低延迟模式
                                            currentPracticeType = "words"
                                            showLowLatencyDemo = true
                                        }
                                    }
                                },
                                onSettingsClick = {
                                    // 处理设置按钮点击
                                    // TODO: 显示设置界面
                                },
                                onAboutClick = {
                                    // 处理关于按钮点击
                                    // TODO: 显示关于界面
                                },
                                onExitClick = {
                                    // 处理退出按钮点击
                                    finish()
                                },
                                onMultimodalGemmaClick = {
                                    // 处理多模态Gemma模型演示按钮点击
                                    showMultimodalGemmaDemo = true
                                },
                                modifier = Modifier.padding(innerPadding)
                            )
                        }
                    }
                }
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun MainActivityPreview() {
    一年级书法Theme {
        var showWritingCardDemo by remember { mutableStateOf(false) }
        var showLowLatencyDemo by remember { mutableStateOf(false) }
        var showMiniCPMVDemo by remember { mutableStateOf(false) }
        var currentPracticeType by remember { mutableStateOf("words") } // 默认为词语练习
        
        when {
            showMiniCPMVDemo -> {
                SimplifiedWritingDemo(
                    onBackClick = { showMiniCPMVDemo = false }
                )
            }
            showLowLatencyDemo -> {
                LowLatencyWritingCardDemo(
                    onBackClick = { showLowLatencyDemo = false },
                    practiceType = currentPracticeType
                )
            }
            showWritingCardDemo -> {
                WritingCardDemo(
                    onBackClick = { showWritingCardDemo = false }
                )
            }
            else -> {
                WelcomeScreen(
                    onPracticeTypeSelected = {},
                    onSettingsClick = {},
                    onAboutClick = {},
                    onExitClick = {}
                )
            }
        }
    }
}